<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('新增通知公告')"/>
    <th:block th:include="include :: datetimepicker-css"/>
    <th:block th:include="include :: summernote-css"/>
</head>
<body class="white-bg">
<div id="app" class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-gonggao-add">
        <div class="form-group">
            <label class="col-sm-3 control-label">公告标题：</label>
            <div class="col-sm-8">
                <input name="gonggaobiaoti" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">公告类型：</label>
            <div class="col-sm-8">
                <select name="gonggaoleixing" class="form-control" th:with="type=${@dict.getType('sys_gonggao_type')}">
                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">公告内容：</label>
            <div class="col-sm-8">
                <input id="gonggaoneirong" name="gonggaoneirong" type="hidden" class="form-control">
                <div class="summernote"></div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">公告状态：</label>
            <div class="col-sm-8">
                <select name="gonggaozhuangtai" class="form-control"
                        th:with="type=${@dict.getType('sys_gonggao_status')}">
                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">是否置顶：</label>
            <div class="col-sm-8">
                <select name="beizhu1" class="form-control">
                    <option value="1">置顶</option>
                    <option value="0">不置顶</option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">接收人：</label>
            <div class="col-sm-8">
                <div class="input-group">
                    <input name="jieshouren" onclick="selectJieshourenTree()" id="jieshourennames" type="text"
                           placeholder="所有人" class="form-control" value="所有人">
                    <input name="jieshourenid" id="jieshourenids" class="form-control" style="display:none" value="0">
                    <span class="input-group-addon"><i class="fa fa-search"></i></span>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">文件上传：</label>
            <div class="col-sm-8">
                <input id="filePath" class="form-control" type="file" accept="image/*,application/msword,text/plain,application/pdf,application/vnd.ms-excel,
                    application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-powerpoint,application/vnd.openxmlformats-officedocument.presentationml.presentation">
            </div>
        </div>

    </form>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: datetimepicker-js"/>
<th:block th:include="include :: summernote-js"/>
<script th:inline="javascript">
    var prefix = ctx + "system/gonggao"


    /* 通知公告-新增-选择接收人 */
    function selectJieshourenTree() {
        $.modal.open("选择接收人", "/system/gonggao/JieshourenTree", 1400 ,800,doSubmit);
    }

    function doSubmit(index, layero) {
        var iframeWin = $(layero).find("iframe")[0].contentWindow //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method()
        var rows = iframeWin.getSelections();
        var jieshourenids=new Array();
        var jieshourennames=new Array();
        if(rows.length>0){
            for (var i = 0; i < rows.length; i++) {
                jieshourenids.push(rows[i]["userId"]);
                jieshourennames.push(rows[i]["userName"]);
            }
            $("#jieshourenids").val(jieshourenids);
            $("#jieshourennames").val(jieshourennames);
        }else {
            $("#jieshourenids").val('0');
            $("#jieshourennames").val('所有人');
        }
        $.modal.close(index);
    }

    $('.summernote').summernote({
        placeholder: '请输入公告内容',
        height: 192,
        lang: 'zh-CN',
        followingToolbar: false,
        dialogsInBody: true,
        callbacks: {
            onImageUpload: function (files) {
                sendFile(files[0], this);
            }
        }
    });


    // 公告内容中加图片
    function sendFile(file, obj) {
        var data = new FormData();
        data.append("file", file);
        $.ajax({
            type: "POST",
            url: ctx + "common/upload",
            data: data,
            cache: false,
            contentType: false,
            processData: false,
            dataType: 'json',
            success: function (result) {
                if (result.code == web_status.SUCCESS) {
                    $(obj).summernote('editor.insertImage', result.url, result.fileName);
                } else {
                    $.modal.alertError(result.msg);
                }
            },
            error: function (error) {
                $.modal.alertWarning("图片上传失败。");
            }
        });
    }

    //文件上传
    function uploadFile() {
        var formData = new FormData();
        formData.append('file', $('#filePath')[0].files[0]);
        var res = true
        $.ajax({
            url: ctx + "common/upload",
            type: 'post',
            cache: false,
            data: formData,
            processData: false,
            contentType: false,
            dataType: "json",
            async: false,
            success: function (result) {
                if (result.code == web_status.SUCCESS) {
                    var fujianurl = result.url
                    $('#filePath').attr("fujianurl", fujianurl)
                } else {
                    layer.alert(result.msg);
                    res = false
                }
            },
        });
        return res
    }


    $("#form-gonggao-add").validate({
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            var sHTML = $('.summernote').summernote('code');
            $("#gonggaoneirong").val(sHTML);
            //有上传文件添加fujianurl
            var formdata = $('#form-gonggao-add').serialize()
            console.log(formdata)
            if ($('#filePath')[0].files[0] != null) {
                var result = uploadFile()
                console.log(result)
                if (result == false) {
                    return
                }
                formdata += "&fujianurl=" + $('#filePath').attr('fujianurl');
            }


            $.operate.save(prefix + "/add", formdata);

        }
    }


</script>
</body>
</html>